using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Perovich.GameObjects.Interfaces;
using System.Globalization;

namespace Perovich.GameObjects.Controls
{

    /// <summary>
    /// The <see cref="FpsCounter"/> class is a <see cref="TextLabel"/> that displays the current Fps based on a format string.
    /// </summary>
    public class FpsCounter : TextLabel
    {
        private string format;
        private float elapsedTime;
        private int frameCounter;
        private int FPS;

        /// <summary>
        /// Gets or sets the format string.
        /// </summary>
        public string Format
        {
            get
            {
                return format;
            }
            set
            {
                format = value;
            }
        }

        /// <summary>
        /// Constructs a new instance of the <see cref="FpsCounter"/> class.
        /// </summary>
        /// <param name="game">
        ///     <see cref="Perovich.GameObjects.Interfaces.ICanHoldComponents"/> instance that this <see cref="FpsCounter"/> will be constructed for.
        /// </param>
        public FpsCounter(ICanHoldComponents game)
            : base(game)
        {
            format = "{0}";
        }


        /// <summary>
        /// Allows the <see cref="FpsCounter"/> to update itself.
        /// </summary>
        /// <param name="gameTime">
        ///     Provides a snapshot of the game's timing values.
        /// </param>
        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            if (gameTime == null)
                throw new ArgumentNullException("gameTime");
            elapsedTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
            frameCounter++;

            if (elapsedTime > 1)
            {
                FPS = frameCounter;
                frameCounter = 0;
                elapsedTime = 0;
            }

            Text = string.Format(CultureInfo.CurrentCulture ,format, FPS);
            base.Update(gameTime);
        }
    }
}
